package com.dlengine.framework.web.vo;

import com.dlengine.framework.web.enums.ResponseCode;
import com.dlengine.framework.web.base.response.BaseResponse;
import com.dlengine.framework.web.base.response.BaseResponseCode;
import io.swagger.annotations.ApiModelProperty;

/**
 * ResponseVO
 *
 * @author chentudong
 * @date 2024/1/15 11:35
 * @since 1.0
 */
public class ResponseVO<T> implements BaseResponse<T> {

  /**
   * data
   */
  @ApiModelProperty("data")
  private T data;

  /**
   * code
   */
  @ApiModelProperty("状态码")
  private Integer code;

  /**
   * message
   */
  @ApiModelProperty("提示信息")
  private String message;

  /**
   * 成功/失败
   */
  @ApiModelProperty("success")
  private Boolean success;

  /**
   * 分页详情
   */
  @ApiModelProperty("分页详情")
  private PageInfoVO pageInfo;

  /**
   * 请求时间
   */
  @ApiModelProperty("请求时间")
  private Long datetime;

  /**
   * 链路追踪id
   */
  @ApiModelProperty("链路追踪id")
  private String traceId;

  public ResponseVO() {
  }

  public ResponseVO(Integer code, String message, Boolean success, Long datetime) {
    this.code = code;
    this.message = message;
    this.success = success;
    this.datetime = datetime;
  }

  public ResponseVO(T data, Integer code, String message, Boolean success, Long datetime) {
    this.data = data;
    this.code = code;
    this.message = message;
    this.success = success;
    this.datetime = datetime;
  }

  public ResponseVO(T data, Integer code, String message, Boolean success, Long datetime,
      String traceId) {
    this.data = data;
    this.code = code;
    this.message = message;
    this.success = success;
    this.datetime = datetime;
    this.traceId = traceId;
  }

  public ResponseVO(T data, Integer code, String message, Boolean success, PageInfoVO pageInfo,
      Long datetime, String traceId) {
    this.data = data;
    this.code = code;
    this.message = message;
    this.success = success;
    this.pageInfo = pageInfo;
    this.datetime = datetime;
    this.traceId = traceId;
  }

  /**
   * succeed
   *
   * @param data       data
   * @param page       page
   * @param size       size
   * @param totalPage  totalPage
   * @param totalCount totalCount
   * @param <T>        <T>
   * @return ResponseVO<T>
   */
  public static <T> ResponseVO<T> succeed(T data, Integer page, Integer size, Integer totalPage,
      Long totalCount) {
    PageInfoVO pageInfo = new PageInfoVO();
    pageInfo.setPage(page);
    pageInfo.setSize(size);
    pageInfo.setTotalPage(totalPage);
    pageInfo.setTotalCount(totalCount);
    return new ResponseVO<>(data, ResponseCode.SUCCESS.code(), ResponseCode.SUCCESS.message(),
        ResponseCode.SUCCESS.success(), pageInfo, System.currentTimeMillis(), null);
  }


  /**
   * succeed
   *
   * @param <T> <T>
   * @return ResponseVO<T>
   */
  public static <T> ResponseVO<T> succeed() {
    return new ResponseVO<>(ResponseCode.SUCCESS.code(), ResponseCode.SUCCESS.message(),
        ResponseCode.SUCCESS.success(), System.currentTimeMillis());
  }

  /**
   * response
   *
   * @param data         data
   * @param responseCode responseCode
   * @param pageInfo     pageInfo
   * @param <T>          <T>
   * @return ResponseVO<T>
   */
  public static <T> ResponseVO<T> response(T data, BaseResponseCode responseCode,
      PageInfoVO pageInfo) {
    return new ResponseVO<>(data, responseCode.code(), responseCode.message(),
        responseCode.success(), pageInfo, System.currentTimeMillis(), null);
  }


  /**
   * response
   *
   * @param data         data
   * @param responseCode responseCode
   * @param <T>          <T>
   * @return ResponseVO<T>
   */
  public static <T> ResponseVO<T> response(T data, BaseResponseCode responseCode) {
    return new ResponseVO<>(data, responseCode.code(), responseCode.message(),
        responseCode.success(), System.currentTimeMillis());
  }

  /**
   * response
   *
   * @param responseCode responseCode
   * @param <T>          <T>
   * @return ResponseVO<T>
   */
  public static <T> ResponseVO<T> response(BaseResponseCode responseCode) {
    return new ResponseVO<>(null, responseCode.code(), responseCode.message(),
        responseCode.success(), System.currentTimeMillis());
  }

  /**
   * response
   *
   * @param code    code
   * @param message message
   * @param <T>     <T>
   * @return ResponseVO<T>
   */
  public static <T> ResponseVO<T> failed(Integer code, String message) {
    return new ResponseVO<>(null, code, message,
        false, System.currentTimeMillis());
  }


  @Override
  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  @Override
  public Integer getCode() {
    return code;
  }

  public void setCode(Integer code) {
    this.code = code;
  }

  @Override
  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  @Override
  public Boolean getSuccess() {
    return success;
  }

  public void setSuccess(Boolean success) {
    this.success = success;
  }

  @Override
  public PageInfoVO getPageInfo() {
    return pageInfo;
  }

  public void setPageInfo(PageInfoVO pageInfo) {
    this.pageInfo = pageInfo;
  }

  @Override
  public Long getDatetime() {
    return datetime;
  }

  public void setDatetime(Long datetime) {
    this.datetime = datetime;
  }

  @Override
  public String getTraceId() {
    return traceId;
  }

  public void setTraceId(String traceId) {
    this.traceId = traceId;
  }
}
